
#include <string.h>
#include "ka-io/stream.h"

using namespace io;

IStream::~IStream() {
    
}

MemoryStream::MemoryStream(void *start, size_t size) {
    _start = (uint8_t *)start;
    _end = _start + size;
    _offset = 0;
}

MemoryStream::~MemoryStream() {
}

size_t MemoryStream::getLength() const {
    return (size_t)(_end-_start);
}

size_t MemoryStream::getPosition() const {
    return _offset;
}

size_t MemoryStream::read(void *dest, size_t byteCount) {
    size_t remains = getLength()-_offset;
    size_t copyCount = remains < byteCount ? remains : byteCount;
    memmove(dest, _start+_offset, copyCount);
    _offset += copyCount;
    return copyCount;
}

size_t MemoryStream::write(const void *source, size_t byteCount) {
    size_t remains = getLength()-_offset;
    size_t copyCount = remains < byteCount ? remains : byteCount;
    memmove(_start, (const uint8_t *)source+_offset, copyCount);
    _offset += copyCount;
    return copyCount;
}

void MemoryStream::close() {
    
}
